﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using CodePlex.DnD4eCampaignManager.DnD4e;
using CodePlex.DnD4eCampaignManager.DnD4e.Interfaces;
using CodePlex.DnD4eCampaignManager.Repositories;
using CodePlex.DnD4eCampaignManager.Repositories.XmlRepositories;
using NUnit.Framework;

namespace CodePlex.DnD4eCampaignManager.CampaignManagerTests.RepositoryTests
{
    [TestFixture]
    public class LocationXmlRepositoryTests
    {
        private Location _location;

        [Test]
        public void LocationRepositoryCanGetTheListOfLocationNames()
        {
            ILocationRepository repository = new LocationXmlRepository();

            ReadOnlyCollection<string> locationNames = repository.GetAllNames();

            // Read the list of location names from the file system
            List<string> fileSystemNames = new List<string>();
            foreach ( string fileName in Directory.GetFiles("Locations\\") )
            {
                if ( fileName != string.Empty )
                {
                    FileInfo fileInfo = new FileInfo(fileName);
                    fileSystemNames.Add(fileInfo.Name.Replace(fileInfo.Extension, string.Empty));
                }
            }

            // Verify the two lists match
            for ( int locationIndex = 0; locationIndex < fileSystemNames.Count; locationIndex++ )
            {
                Assert.AreEqual(fileSystemNames[locationIndex], locationNames[locationIndex]);
            }
        }

        [Test]
        public void LocationRepositoryCanSaveALocationAndLoadIt()
        {
            TestLocation();
            ILocationRepository repository = new LocationXmlRepository();

            // Save the location
            repository.Save(_location);

            // Load the location
            ILocation loadedLocation = repository.GetByName(_location.Name);

            // Verify the save
            Assert.AreEqual(_location.Name, loadedLocation.Name);
        }

        private void TestLocation()
        {
            _location = new Location
                            {
                                Name = "Some place nifty"
                            };
        }
    }
}